package S2.server;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import Entity.ContactCustomer;
import Entity.Customer;
import Entity.Product;
import Entity.User;
import Message.*;
import Model.*;
import ocsf.server.*;

public class S2Server extends AbstractServer {
	  /**
	   * The default port to listen on.
	   */
	  final public static int DEFAULT_PORT = 5555;
	  public ConnectionManage connDB;
	  private S2ServerUI serverUI;
	  
	  
	  public S2Server(S2ServerUI serverUI)
	  {
		  super();
		  this.serverUI = serverUI;
		  serverUI.setVisible(true);
	  }
	 
	  
	  public void handleMessageFromClient
	    (Object msg, ConnectionToClient client)
	  {
		  Message clientMsg = (Message)msg;
			try 
			{
				switch(clientMsg.getMsg()){
				case "UserLogIn":
					userLogIn(clientMsg);
					break;
				case "Add Customer":
					addCustomer(clientMsg);
				
				case "Add Product":
					addProduct(clientMsg);
				
				case "Add Contact Customer":
					addContactClient(clientMsg);
				default:
					break;
				}
				
			} catch (Exception e) {e.printStackTrace();}
			
			try {
				
				//client.sendToClient(clientMsg);
				client.sendToClient(clientMsg);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	  }

	private void addContactClient(Message msg) throws SQLException, IOException {
		ContactCustomer contactCustomer = (ContactCustomer)msg.getObj();
		boolean rs = ContactCusModel.addContactCus(contactCustomer.getCustomerId(), contactCustomer.employeeId
				,contactCustomer.getDate(),contactCustomer.getStartTime(), 
				contactCustomer.getDescription(),contactCustomer.getEndTime());
		msg.setMsg("Add Product");
		msg.setObj(rs);
		msg.setStatus(true);
		msg.setStatusMsg("done");	
		
	}


	private void addProduct(Message msg) throws SQLException, IOException {
		Product product = (Product)msg.getObj();
		boolean rs = ProductModel.addProduct(product.getId(), product.getName(),
				product.getDescription(),
				product.getPrice(), product.getPicture());
		msg.setMsg("Add Product");
		msg.setObj(rs);
		msg.setStatus(true);
		msg.setStatusMsg("done");	
		
		
	}


	private void userLogIn(Message msg) throws SQLException {
		User user = (User) msg.getObj();
		user= UsersModel.getUser(user.getName(), user.getPassword());
		if(user == null)
			msg.setObj(null);
		else
			msg.setObj(user);
		msg.setStatus(true);
		msg.setStatusMsg("done");
	}

	private void addCustomer(Message msg) throws SQLException{
		Customer customer = (Customer) msg.getObj();
		boolean rs= CustomerModel.addCustomer(customer.getId(),customer.getName(), customer.getbirthDate(), customer.getRate());
		msg.setMsg("Add Customer");
		msg.setObj(rs);
		msg.setStatus(true);
		msg.setStatusMsg("done");
	}
}
